package com.ehome.poct.core.dao.mybatis;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ehome.mybatisplus.support.AbstractCrudRepository;
import com.ehome.poct.core.api.dto.check.CheckStatusDTO;
import com.ehome.poct.core.api.dto.order.TestOrderCheckRecordDTO;
import com.ehome.poct.core.dao.entity.order.TestOrderCheckRecordDO;
import com.ehome.poct.core.dao.mybatis.mapper.order.TestOrderCheckRecordMapper;
import com.ehome.poct.core.repository.order.TestOrderCheckRecordRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;

/**
 * 检测订单检测记录RepositoryImpl
 *
 * @author muqiu
 * @Date 2022-10-11 13:41
 */
@Repository
@RequiredArgsConstructor
public class TestOrderCheckRecordRepositoryImpl extends AbstractCrudRepository<TestOrderCheckRecordDO, TestOrderCheckRecordDTO> implements TestOrderCheckRecordRepository {
    @Autowired private TestOrderCheckRecordMapper testOrderCheckRecordMapper;
    @Override
    public void updateByOrderNo(CheckStatusDTO checkStatusDTO) {
        //检测中
        if(checkStatusDTO.getStatus() == 27){
            testOrderCheckRecordMapper.update(null, Wrappers.<TestOrderCheckRecordDO>lambdaUpdate().set(TestOrderCheckRecordDO::getStatus,0).eq(TestOrderCheckRecordDO::getOrderNo,checkStatusDTO.getOrderNo()).eq(TestOrderCheckRecordDO::getTestType,checkStatusDTO.getDeviceType()));
        }else {
            //检测完成
            testOrderCheckRecordMapper.update(null, Wrappers.<TestOrderCheckRecordDO>lambdaUpdate().set(TestOrderCheckRecordDO::getStatus,1).eq(TestOrderCheckRecordDO::getOrderNo,checkStatusDTO.getOrderNo()).eq(TestOrderCheckRecordDO::getTestType,checkStatusDTO.getDeviceType()));
        }
    }

    private final TestOrderCheckRecordMapper mapper;
    @Override
    public void updateRemark(String orderNo, Integer type) {
        mapper.update(null, Wrappers.<TestOrderCheckRecordDO>lambdaUpdate()
                .eq(TestOrderCheckRecordDO::getOrderNo, orderNo)
                .eq(TestOrderCheckRecordDO::getTestType, type)
                .set(TestOrderCheckRecordDO::getUpdateTime, LocalDateTime.now())
                .set(TestOrderCheckRecordDO::getRemark, "异常重测"));
    }
}
